package com.net.action.sell;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.googlecode.jsonplugin.annotations.JSON;
import com.net.model.Commodity;
import com.net.model.Customer;
import com.net.model.SellBackDetail;
import com.net.model.SellBackMain;
import com.net.service.sell.SellBackMainService;
import com.opensymphony.xwork2.ActionSupport;

@Component("sellBackAction")
@Scope("prototype")
public class SellBackMainAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private SellBackMain sellBackMain;
	private SellBackDetail sellBackDetail;
	private Customer customer;
	private Commodity commodity;
	private SellBackMainService sellBackMainService;

	private List<SellBackMain> sellBackMains;
	private List<SellBackDetail> sellBackDetails;
	private List<Commodity> commoditys;
	private String[] selectedList;

	/** 以上为全局变量的声明 **/

	public String listSellBackMain() {
		System.out.println("enter!!");
		this.sellBackMains = sellBackMainService.getSellBackMains();
		return "listSellBackMains";
	}

	/**
	 * 因为存在事务问题，所以必须把数据传到DAO里save
	 */
	public String addSellBackMain() {
		System.out.println("enter listSellBackMain !!" + selectedList.length
				+ " , " + commoditys.size());
		if (selectedList != null) {
			sellBackMainService.add(sellBackMain,commoditys,customer,sellBackDetails);
		}
		this.sellBackMains = sellBackMainService.getSellBackMains();
		return "SellBackMainAdd";
	}

	public String updateToLists() {
		this.sellBackMain = sellBackMainService.getSellBackMainById(sellBackMain.getId());
		List<SellBackDetail> sellBacks = new ArrayList<SellBackDetail>();
		for(SellBackDetail s : sellBackMain.getSellbackDetail()) {
			System.out.println("s: " + s);
			sellBacks.add(s);
		}
		this.sellBackDetails = sellBacks;
		return "listSellBackToUpdate";
	}
	
	public String updateSellBackMain() {
		sellBackMainService.update(sellBackMain,commoditys,customer,sellBackDetails);
		this.sellBackMains = sellBackMainService.getSellBackMains();
		return "sellBackUpdate";
	}
	
	public String delSellBackMain() {
		//int id = sellBackMain.getId();
		sellBackMainService.delete(sellBackMain);
		this.sellBackMains = sellBackMainService.getSellBackMains();
		return "sellBackMainsDel";
	}
	/*
	 * public String addSellBackMain() {
	 * System.out.println("enter listSellBackMain !!" + selectedList.length
	 * +" , "+commoditys.size()); float price = 0; if (selectedList != null) {
	 * for(int i = 0 ; i < commoditys.size(); i++) { String name =
	 * commoditys.get(i).getCommodity_name(); commodity =
	 * sellBackMainService.getCommodityByName(name); price +=
	 * commodity.getCurrent_price() * sellBackDetails.get(i).getQuantity(); }
	 * System.out.println("customer:　" + customer.getCustomer_name()); customer
	 * = sellBackMainService.getCustomerByName(customer.getCustomer_name());
	 * sellBackMain.setCustomer(customer); sellBackMain.setTotalMoney(price);
	 * sellBackMain.setTypeCount(commoditys.size());
	 * 
	 * sellBackMainService.add(sellBackMain); for(int i = 0 ; i <
	 * commoditys.size(); i++) { String name =
	 * commoditys.get(i).getCommodity_name(); commodity =
	 * sellBackMainService.getCommodityByName(name); SellBackDetail sb = new
	 * SellBackDetail(); sb = sellBackDetails.get(i);
	 * sb.setCommodity(commodity); sb.setSellBackMain(sellBackMain);
	 * sellBackMainService.add(sb); } } System.out.println("sellBackMainId : " +
	 * sellBackMain.getId() ); System.out.println("customer.customer_name : " +
	 * customer.getCustomer_name());
	 * System.out.println("commoditys[0].commodity_name : " +
	 * commoditys.get(0).getCommodity_name() );
	 * System.out.println("sellBackDetails[0].quantity : " +
	 * sellBackDetails.get(0).getQuantity() ); this.sellBackMains =
	 * sellBackMainService.getSellBackMains(); return "SellBackMainAdd"; }
	 */
	/** 以下是变量的get/set 方法 **/

	public SellBackMain getSellBackMain() {
		return sellBackMain;
	}

	public void setSellBackMain(SellBackMain sellBackMain) {
		this.sellBackMain = sellBackMain;
	}

	public SellBackDetail getSellBackDetail() {
		return sellBackDetail;
	}

	public void setSellBackDetail(SellBackDetail sellBackDetail) {
		this.sellBackDetail = sellBackDetail;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public Commodity getCommodity() {
		return commodity;
	}

	public void setCommodity(Commodity commodity) {
		this.commodity = commodity;
	}

	@JSON(serialize = false)
	public SellBackMainService getSellBackMainService() {
		return sellBackMainService;
	}

	@Resource(name = "sellBackMainService")
	public void setSellBackMainService(SellBackMainService sellBackMainService) {
		this.sellBackMainService = sellBackMainService;
	}

	public List<SellBackMain> getSellBackMains() {
		return sellBackMains;
	}

	public void setSellBackMains(List<SellBackMain> sellBackMains) {
		this.sellBackMains = sellBackMains;
	}

	public List<SellBackDetail> getSellBackDetails() {
		return sellBackDetails;
	}

	public void setSellBackDetails(List<SellBackDetail> sellBackDetails) {
		this.sellBackDetails = sellBackDetails;
	}

	public List<Commodity> getCommoditys() {
		return commoditys;
	}

	public void setCommoditys(List<Commodity> commoditys) {
		this.commoditys = commoditys;
	}

	public String[] getSelectedList() {
		return selectedList;
	}

	public void setSelectedList(String[] selectedList) {
		this.selectedList = selectedList;
	}
}
